package by.fedorenko.sample.services.impl;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.fedorenko.sample.dataaccess.InvoiceDao;
import by.fedorenko.sample.datamodel.Invoice;
import by.fedorenko.sample.services.InvoiceService;

public class InvoiceServiceImpl implements InvoiceService {
	private static final Logger LOGGER = LoggerFactory.getLogger(InvoiceServiceImpl.class);

	@Inject
	private InvoiceDao invoiceDao;

	@Override
	public Invoice get(Long id) {
		Invoice entity = invoiceDao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Invoice invoice) {
		if (invoice.getId() == null) {
			LOGGER.debug("Save new: {}", invoice);
			invoiceDao.insert(invoice);
		} else {
			LOGGER.debug("Update: {}", invoice);
			invoiceDao.update(invoice);
		}
	}

	@Override
	public void delete(Invoice invoice) {
		LOGGER.debug("Remove: {}", invoice);
		invoiceDao.delete(invoice.getId());
	}


}
